﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using SBACN.WebSite.Models;
using SBACN.WebSite.Cache;
using System.Text;
using SBACN.WebSite.Code;

namespace SBACN.WebSite.En
{
	public partial class EnWebMap : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			BindData();
		}

		protected void BindData()
		{
			List<TB_SystemMap> MList = new List<TB_SystemMap>();
			var resultM = SystemMapCache.Instance.List.Where(p => (p.ShowInMap && p.Code.Length == 4)).ToList();

			foreach (var l in resultM)
			{
				var resultSM = SystemMapCache.Instance.List.Where(p => (p.Code.Length == 8 && p.Code.StartsWith(l.Code) && p.EURL != "")).ToList();
				if (resultSM.Count > 0)
				{
					MList.Add(l);
				}
			}

			this.Map.DataSource = MList;
			this.Map.DataBind();
		}

		protected string Second(string Code)
		{
			var resultM = SystemMapCache.Instance.List.Where(p => (p.Code.Length == 8 && p.Code.StartsWith(Code) && p.ShowInMap && p.EURL !="")).ToList();

			if (resultM.Count > 0)
			{
				StringBuilder Html = new StringBuilder();
				foreach (var l in resultM)
				{
					string url = "";

					// TODO 外链去code

					Html.Append("<p>");
					//url 
					if (string.IsNullOrWhiteSpace(l.EURL))
					{
						// 当前code的下一级url
						url = MUrl(l.Code);
					}
					else
					{
						url = BasePage.UrlAppendQueryString(l.EURL, "st", l.Code, false);
					}

					if (!string.IsNullOrWhiteSpace(url))
					{
						Html.Append("<a target='_bronk' href='");
						Html.Append(url);
						Html.Append("'>");
						Html.Append(l.EText);
						Html.Append("</a>");
					}
					else
					{
						Html.Append(l.EText);
					}

					Html.Append("</p>");
				}
				return Html.ToString();
			}
			else
			{
				return "";
			}

		}
		//TODO URL
		protected string MUrl(string code)
		{
			var resultM = SystemMapCache.Instance.List.Where(p => (p.Code.Length == 12 && p.Code.StartsWith(code) && p.EURL !="" && p.ShowInMap)).ToList();

			string url = "";
			if (resultM.Count > 0)
			{
				foreach (var l in resultM)
				{
					url = BasePage.UrlAppendQueryString(l.EURL, "st", l.Code, false);
					break;
				}
			}
			else
			{
				url = "";
			}

			return url;
		}



	}
}